package us.quizgame;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import java.awt.EventQueue;
import java.awt.SystemColor;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextPane;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComboBox;

import us.quizgame.database.Database;
import us.quizgame.database.Score;
import us.quizgame.quiz.QuizGame;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;


public class ScoreCard extends JFrame {
	
	Database db = new Database();
	private QuizGame quizGame;
	private boolean isCompletedQuiz;
	
	private JPanel contentPane;
	private JTextField submitNameTextfield;
	private JLabel scoreLabel;
	private JLabel finalTimeLabel;
	private JTextField timeField;
	private JTextField scoreField;
	private JButton submitQuizButton;
	private JButton reviewQuizButton;
	JComboBox gameMode;
	JList highScores;
	private int score;
	private double finalTime;
	
	Score[] topScores;
	
	//black magic to make a class initialize its self
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ScoreCard frame = new ScoreCard();
					frame.setVisible(true);
					frame.setLocationRelativeTo(null);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	
	//Default constructor builds a ScoreCard without scores
	public ScoreCard() {
		setResizable(false);
		
		isCompletedQuiz = false;
		
		setBackground(Color.BLACK);
		setTitle("The XTR3M3 Quiz Game");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 550, 450);
		contentPane = new JPanel();
		contentPane.setBackground(Color.BLACK);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		submitNameTextfield = new JTextFieldLimit(20);
		submitNameTextfield.setBackground(SystemColor.text);
		submitNameTextfield.setText("Enter your name");
		submitNameTextfield.setForeground(SystemColor.inactiveCaptionText);
		submitNameTextfield.setBounds(10, 136, 250, 25);
		submitNameTextfield.setColumns(10);
		
		
		scoreLabel = new JLabel("Score");
		scoreLabel.setForeground(Color.WHITE);
		scoreLabel.setHorizontalAlignment(SwingConstants.CENTER);
		scoreLabel.setBounds(270, 178, 134, 35);
		
		
		finalTimeLabel = new JLabel("Quiz Time");
		finalTimeLabel.setForeground(Color.WHITE);
		finalTimeLabel.setHorizontalAlignment(SwingConstants.CENTER);
		finalTimeLabel.setBounds(270, 131, 134, 35);
		
		
		submitQuizButton = new JButton("Submit Quiz");
		submitQuizButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//submit score to database
				submitQuiz();
			}
		});
		submitQuizButton.setBounds(10, 184, 250, 35);
		
		
		reviewQuizButton = new JButton("Review Quiz");
		reviewQuizButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				//launch reviewQuiz screen
				reviewQuiz();
			}
		});
		reviewQuizButton.setBounds(390, 378, 125, 23);
		// reviewQuizButton.setBounds(424, 378, 100, 23);
		
		JLabel topScoreLabel = new JLabel("Top Scores");
		topScoreLabel.setForeground(Color.WHITE);
		topScoreLabel.setHorizontalAlignment(SwingConstants.LEFT);
		topScoreLabel.setBounds(10, 218, 88, 25);
		contentPane.add(topScoreLabel);
		
		JButton btnGameOver = new JButton("Game Over! (return to menu)");
		btnGameOver.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//close ScoreCard window
				close();
			}
		});
		btnGameOver.setBounds(126, 375, 222, 29);
		//btnGameOver.setBounds(146, 375, 222, 29);
		contentPane.add(btnGameOver);
		
		
		
		timeField = new JTextField();
		timeField.setEditable(false);
		timeField.setBounds(405, 138, 86, 20);
		timeField.setColumns(10);
		
		
		scoreField = new JTextField();
		scoreField.setEditable(false);
		scoreField.setBounds(405, 185, 86, 20);//
		scoreField.setColumns(10);
		
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon(Menu.class.getResource("/us/quizgame/images/av-20034.png")));
		lblNewLabel.setBounds(194, 6, 150, 99);
		contentPane.add(lblNewLabel);
		
		highScores = new JList();
		JScrollPane scrollPane = new JScrollPane(highScores);
		scrollPane.setBounds(10, 254, 514, 110);
		contentPane.add(scrollPane);
		
		gameMode = new JComboBox(QuizGame.gameModes);
		gameMode.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				displayHighScores(gameMode.getSelectedItem().toString());
			}
		});
		gameMode.setBounds(342, 220, 182, 20);
		contentPane.add(gameMode);
		
		//default to showing the scores from the normal quiz game
		displayHighScores("normal");
		
		
	}
	
	public ScoreCard(QuizGame q)
	{
		this();
		this.finalTime = q.totalGameTime;
		this.score = q.score;
		this.quizGame = q;
		this.isCompletedQuiz = true;
		
		//deals with scores
		displayHighScores(q.gameMode);
				
		//Show hidden text fields if ScoreCard is loaded from an actual game
		showTextFields();
		
		gameMode.setVisible(false);
	}
	
	private void displayHighScores(String mode)
	{
		highScores.setListData(db.fetchTopScores(20, mode));
	}


	//shows text fields that are otherwise hidden to prevent abuse
	private void showTextFields() {
		
		//Adds time field to window
		contentPane.add(timeField);
		contentPane.add(finalTimeLabel);
		timeField.setText( Double.toString(finalTime));
		
		
		//Adds score field to window
		contentPane.add(scoreField);
		contentPane.add(scoreLabel);
		scoreField.setText( Integer.toString(score));
		
		//Adds name field to window
		contentPane.add(submitNameTextfield);
		
		//Adds submit and review buttons to window
		contentPane.add(submitQuizButton);
		contentPane.add(reviewQuizButton);
	}
	
	
	public String getName(){
		return submitNameTextfield.getText();
	}
	
	//submits score to database
	public void submitQuiz() {
		
		//convert strings from text fields
		Integer score = new Integer(scoreField.getText());
		Double time = new Double(timeField.getText());
		
		//send score to database
		db.inputScore(submitNameTextfield.getText(), score, time.doubleValue(), quizGame.gameMode);
		
		//update the high score list after submitted
		displayHighScores(quizGame.gameMode);
		
		//they shouldn't be allowed to submit afterwards
		submitQuizButton.setEnabled(false);
	}
	
	private void reviewQuiz()
	{
		//launch ReviewQuiz window
		ReviewQuiz reviewQuiz = new ReviewQuiz(quizGame);
		reviewQuiz.setVisible(true);
		dispose();
	}
	
	private void close() {
		Menu.main(null);
		if (isCompletedQuiz)
			quizGame.dispose(); //quizGame doesn't dispose its self
		dispose();
	}
}
